package com.ihr360.payroll.dao;

import com.ihr360.payroll.entity.salary.SimpleSystemSalaryField;
import com.ihr360.payroll.model.dto.salary.meta.system.SystemSalaryField;
import com.ihr360.rest.reposity.SimpleRestReposity;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource
public interface SystemSalaryFieldRepository extends SimpleRestReposity<SystemSalaryField, Long> {

    @Override
   // @Cacheable("systemSalaryFields")
    Iterable<SystemSalaryField> findAll();

    @Query(value = "select new com.ihr360.payroll.entity.salary.SimpleSystemSalaryField(code,name) from SystemSalaryField")
    List<SimpleSystemSalaryField> getAllSystemSalaryFields();

    @Query(value = "select name from SystemSalaryField where code=:code")
    String getNameByCode(@Param(value = "code")String code);

    @Query(value = "select field from SystemSalaryField field where field.category.id = :categoryId or (field.category is null and field.type.category.id = :categoryId)")
    Page<SystemSalaryField> findByCategory(@Param("categoryId") Long categoryId, Pageable pageable);

    List<SystemSalaryField> findByIdNot(Long excludeId);

    SystemSalaryField findByCode(String code);

    List<SystemSalaryField> findByCodeIn(List<String> companyInitSalaryProfileCodes);

    @Query(value = "select field from SystemSalaryField field where field.category.id = :categoryId or (field.category is null and field.type.category.id = :categoryId)")
    List<SystemSalaryField> findByCategory(@Param("categoryId") Long categoryId);

    @Query(value = " select field from SystemSalaryField field where field.type.id = :salaryFieldTypeId ")
    List<SystemSalaryField> findByType(@Param("salaryFieldTypeId") Long salaryFieldTypeId);

    List<SystemSalaryField> findByCountFieldIs(boolean countField);

    @Query(value = " select field from SystemSalaryField field where type.code = 'statistics' and code like '%total' ")
    List<SystemSalaryField> getAllSystemSalaryCountField();

    @Query(value = " select field from SystemSalaryField field where field.type.id = :salaryFieldTypeId and showInStaffSalary is true ")
    List<SystemSalaryField> findByTypeAndCanShowInStaffSalary(@Param("salaryFieldTypeId") Long salaryFieldTypeId);

    @Query(value = " select field from SystemSalaryField field where field.type.id = :salaryFieldTypeId and showInStaffSalary is not true ")
    List<SystemSalaryField> findByTypeAndCanNotShowInStaffSalary(@Param("salaryFieldTypeId") Long salaryFieldTypeId);

    @Query(value = " select field from SystemSalaryField field where showInStaffSalary is true ")
    List<SystemSalaryField> findCanShowInStaffSalary();

}